package com.jxtti.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.AdvisoryMsg;
import com.jxtti.model.User;
import com.jxtti.service.AdvisoryMsgService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.ExceptionUtil;

/**
 * 咨询留言 bff
 */
@Controller
@RequestMapping("/advisoryMsgController")
public class AdvisoryMsgController {

	private final static Logger log = Logger.getLogger(AdvisoryMsgController.class);

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@Autowired
	private AdvisoryMsgService advisoryMsgService;
	
	
	//链接到页面
	@RequestMapping(value = "/netComplaint")
	public ModelAndView netComplaint(@RequestParam Map<String, Object> data,AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		try {
			// 查找总数、已回复数、待回复数目、超15天未回复数		
			advisoryMsg = advisoryMsgService.queryStatistics(advisoryMsg);
			data.put("statistics", advisoryMsg);			
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/interaction/netComplaint.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	@RequestMapping(value="/chart1Analysis") 
	@ResponseBody
	public AdvisoryMsg  chart1Analysis(@RequestParam Map<String, Object> data,AdvisoryMsg advisoryMsg,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{					
		advisoryMsg = advisoryMsgService.queryChart1Statistics(advisoryMsg); 		
		return advisoryMsg;
	}
	
	@RequestMapping(value="/chart2Analysis") 
	@ResponseBody
	public AdvisoryMsg  chart2Analysis(@RequestParam Map<String, Object> data,AdvisoryMsg advisoryMsg,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{					
		advisoryMsg = advisoryMsgService.queryChart2Statistics(advisoryMsg); 		
		return advisoryMsg;
	}
	
	@RequestMapping(value="/chart3Analysis") 
	@ResponseBody
	public AdvisoryMsg  chart3Analysis(@RequestParam Map<String, Object> data,AdvisoryMsg advisoryMsg,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{					
		advisoryMsg = advisoryMsgService.queryChart3Statistics(advisoryMsg); 		
		return advisoryMsg;
	}
		
		

	/**
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(@RequestParam Map<String, Object> data,
			AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/interaction/advisory_msg_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 获取数据.
	 * 
	 * @param data
	 * @param siteAd
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listData")
	@ResponseBody
	public Map<String, Object> listData(@RequestParam Map<String, Object> data,AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		
		Map<String, Object> result = new HashMap<String, Object>();			
		User userInfo = AuthUtil.getUserInfo(request);
		if (userInfo.getDistrictId() == null || userInfo.getDistrictId().equals("0")) { // 非超管
			data.put("task_org_id", userInfo.getOrgId().toString()); // 非超级管理员 加上权限
		}
		data.put("is_deleted", "0");
		// 判断是否有时间 ，默认是今天
		if ((data.get("startTime") == null || data.get("startTime").equals(""))	&& (data.get("endTime") == null || data.get("endTime").equals(""))) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 完整的时间
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, -90); // 一个月
			Date monday = c.getTime();
			String preMonday = sdf.format(monday);
			data.put("startTime", preMonday);
		}

		if (data.get("task_org_id") != null	&& !data.get("task_org_id").equals("")) {
			String[] orgIds = data.get("task_org_id").toString().split(",");
			for (int i = 0; i < orgIds.length; i++) {
				orgIds[i] = orgIds[i].replaceAll(orgIds[i], "'" + orgIds[i]	+ "'");
			}
			String cats = StringUtils.join(orgIds, ",");
			data.put("task_org_id", cats);
		}

		PageInfo<AdvisoryMsg> page = advisoryMsgService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}

	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/advisoryMsg/add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 添加或修改数据
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(@RequestParam Map<String, Object> data,
			AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			// User user = AuthUtil.getUserInfo(request);
			// disStandard.setOperPerson(user.getUserName());
			int exeCount = advisoryMsgService.add(advisoryMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, AdvisoryMsg advisoryMsg,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			String type = data.get("opType").toString();
			advisoryMsg = advisoryMsgService.queryAdvisoryMsgById(advisoryMsg);
			data.put("advisoryMsg", advisoryMsg);
			mav.addObject("data", data);
			if (type.equals("reply")) {
				mav.setViewName("/pages/Admin/interaction/advisory_msg_reply.jsp");
			} else {
				mav.setViewName("/pages/Admin/interaction/advisory_msg_edit.jsp");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 修改
			java.util.Date utilDate = new Date();
			java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

			User userInfo = AuthUtil.getUserInfo(request);
			if (advisoryMsg.getReply_content() != null
					&& !advisoryMsg.getReply_content().equals("")) { // 有回复
				// 获取当前的部门id 和名称
				advisoryMsg.setStates(2);
				advisoryMsg.setReply_org_id(userInfo.getOrgId());
				advisoryMsg.setReply_org_name(userInfo.getOrgName());
				advisoryMsg.setReply_time(sqlDate);
			} else if (advisoryMsg.getTask_org_id() != null
					&& !advisoryMsg.getTask_org_id().equals("")) { // 已分配
				advisoryMsg.setStates(1);
				advisoryMsg.setTask_time(sqlDate);
			} else {
				advisoryMsg.setStates(0);
				advisoryMsg.setTask_time(null);
			}

			int exeCount = advisoryMsgService.update(advisoryMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			AdvisoryMsg advisoryMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			advisoryMsg = advisoryMsgService.queryAdvisoryMsgById(advisoryMsg);
			advisoryMsg.setIs_deleted(1);
			advisoryMsg.setStates(-1);
			int exeCount = advisoryMsgService.update(advisoryMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
